<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class trade extends template_plugin
{
	var $ipsclass;
	
	var $name = "Trading";
	var $multiple = 0;
	
	var $settings = array();
	
	function _load(&$plugin)
	{
		parent::_load($plugin);
		
		if($plugin['link'])
		{
			$this->settings = unserialize($plugin['plugin_config']);
		}
		else
		{
			$this->settings['want_items'] = array_keys($this->ipsclass->cache['market_items']);
		}
	}
	
	function acp_settings()
	{
		$this->ipsclass->parser->parse_smilies   = 1;
		$this->ipsclass->parser->parse_html      = 1;
		$this->ipsclass->parser->parse_bbcode    = 1;
		
		$items = array();
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			$items[] = array($item_id,$item['item_name']);
		}
		
		$html = "";
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>What items allow to be put in the wants in the trade</b>",
															$this->ipsclass->adskin->form_multiselect('want_items[]',$items,$this->settings['want_items'],min(10,count($items))),
													)	);
		
		return $html;
	}
	
	function acp_save_settings(&$input)
	{
		$this->ipsclass->parser->parse_smilies   = 1;
		$this->ipsclass->parser->parse_html      = 1;
		$this->ipsclass->parser->parse_bbcode    = 1;
		
		$save = array();
		$save['want_items'] = $this->ipsclass->input['want_items'] ? $this->ipsclass->input['want_items'] : array();
		
		$input['plugin_config'] = serialize($save);
	}
	
	function acp_install()
	{
		$this->ipsclass->DB->load_cache_file(ROOT_PATH.'sources/market/sql/'.$this->ipsclass->vars['sql_driver'].'_plugin_queries.php','sql_plugin_queries');
		
		$this->ipsclass->DB->build_query_from_cache('table_trade_selling',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_trade_selling_items',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_trade_selling_wants',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_trade_offers',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_trade_offer_items',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_trade_logs',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_trade_log_items',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
	}
	
	function acp_uninstall()
	{
		$this->ipsclass->DB->sql_drop_table( 'market_trade_selling' );
		$this->ipsclass->DB->sql_drop_table( 'market_trade_selling_items' );
		$this->ipsclass->DB->sql_drop_table( 'market_trade_selling_wants' );
		$this->ipsclass->DB->sql_drop_table( 'market_trade_offers' );
		$this->ipsclass->DB->sql_drop_table( 'market_trade_offer_items' );
		$this->ipsclass->DB->sql_drop_table( 'market_trade_logs' );
		$this->ipsclass->DB->sql_drop_table( 'market_trade_log_items' );
	}
	
	function acp_menu()
	{
		return array(
					  1 => array('Active Trades','code=plugin&do=run&link='.$this->plugin['link']),
					  2 => array('Completed Trades','code=plugin&do=run&link='.$this->plugin['link'].'&action=previous'),
					);
	}
	
	function acp()
	{
		
		$redirect = array(
							'current' => 'list_current',
							'detail' => 'trade_details',
							'offers' => 'list_offers',
							'ioffers' => 'list_items_offers',
							'roffers' => 'remove_offer',
							'edit' => 'edit_trade',
							'save' => 'save_trade',
							'remove' => 'remove_trade',
							'previous' => 'list_previous',
							'cdetail' => 'completed_trade_details',
							'coffers' => 'completed_list_offers',
							'cioffers' => 'completed_list_items_offers',
		);
		
		if(!isset($redirect[$this->ipsclass->input['action']]))
		{
			$this->ipsclass->input['action'] = 'current';
		}
		
		$function = 'acp_'.$redirect[$this->ipsclass->input['action']];
		$this->$function();
	}
	
	function acp_list_current()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		$LIMIT = 50;
		
		$this->ipsclass->admin->page_title  = "Welcome to trades list for Trade";
		$this->ipsclass->admin->page_detail = "Welcome to trades list for Trade";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'List Trades' );
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_trade_selling'));
		$page = $this->ipsclass->adskin->build_pagelinks(array(	'TOTAL_POSS'   => $total['total'],
																'PER_PAGE'     => $LIMIT,
																'USE_ST'       => 'st',
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => "Single Page",
																'BASE_URL'     => $this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=current"
														)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->html .= "<br />";
		
		$this->ipsclass->adskin->td_header[] = array( "Name/Starting"     , "" );
		$this->ipsclass->adskin->td_header[] = array( "Seller/IP Address" , "" );
		$this->ipsclass->adskin->td_header[] = array( "# of Items/Offers" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Want/Trade Prices" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Options"           , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Active Trade Listing");
		
		$this->ipsclass->DB->build_query(array(
												'select' => 's.*',
												'from' => array('market_trade_selling'=>'s'),
												'add_join' => array(
																		array(
																				'select' => 'COUNT(i.i_id) as items',
																				'from' => array('market_trade_selling_items'=>'i'),
																				'where' => 'i.t_id=s.t_id',
																				'type' => 'left'
																			),
																		array(
																				'select' => 'm.members_display_name, m.mgroup',
																				'from' => array('members'=>'m'),
																				'where' => 'm.id=s.member_id',
																				'type' => 'left'
																			),
																	),
												'order' => 's.time_selling ASC',
												'group' => 'i.t_id',
												'limit' => array($this->ipsclass->input['st'],$LIMIT),
										)	);
		$q = $this->ipsclass->DB->exec_query();
		$sell = array();
		while($selling = $this->ipsclass->DB->fetch_row($q))
		{
			$selling['offers'] = 0;
			$selling['items'] = $this->ipsclass->do_number_format($selling['items']);
			$selling['selling_money'] = $this->ipsclass->numbers->format_currency($selling['selling_money']);
			$selling['want_money'] = $this->ipsclass->numbers->format_currency($selling['want_money']);
			$selling['member_name'] = $this->ipsclass->market_class->format_name($selling['member_id'],$selling['members_display_name'],$selling['mgroup']);
			$selling['time_selling'] = $this->ipsclass->get_date($selling['time_selling'],'LONG');
			$sell[$selling['t_id']] = $selling;
		}
		if(count($sell) == 0)
		{	
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No active trades",'center');
		}
		else
		{
			$this->ipsclass->DB->build_query(array('select'=>'COUNT(*) as offers, t_id','from'=>'market_trade_offers','where'=>"t_id IN (".implode(", ",array_keys($sell)).")",'group'=>'t_id'));
			$this->ipsclass->DB->exec_query();
			while($offers = $this->ipsclass->DB->fetch_row())
			{
				$sell[$offers['t_id']]['offers'] = $this->ipsclass->do_number_format($offers['offers']);
			}
		}
		
		foreach($sell as $selling)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=detail&amp;sell=".$selling['t_id']."'><b>".$selling['title']."</b></a><br />".$selling['time_selling'],
																				  "Seller: ".$this->ipsclass->market_admin->format_name($selling['member_id'],$selling['members_display_name'],$selling['mgroup'])."<br />IP Address: ".$selling['ip_address'],
																				  "Items: <a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=detail&amp;sell=".$selling['t_id']."'>".$selling['items']."</a><br />Offers: <a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=offers&amp;sell=".$selling['t_id']."'>".$selling['offers']."</a>",
																				  "Trade ".$this->ipsclass->vars['market_currency'].": ".$selling['selling_money']."<br />Wanted ".$this->ipsclass->vars['market_currency'].": ".$selling['want_money'],
																				  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=edit&amp;sell=".$selling['t_id']."'>Edit Trade Details</a><br /><a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=remove&amp;sell=".$selling['t_id']."' onclick='return confirm(\"Are you sure you want to remove this trade?\")'>Remove Trade</a>",
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->admin->output();
	}
	
	function acp_trade_details()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling)
		{
			$this->ipsclass->admin->error("Cannot find trade");
		}
		
		$seller = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$selling['member_id']));
		
		$this->ipsclass->admin->page_title  = "Welcome to trade details for Trade";
		$this->ipsclass->admin->page_detail = "Welcome to trade details for Trade";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'List Trades' );
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Trade Details");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Seller",
																			  $this->ipsclass->market_admin->format_name($selling['member_id'],$seller['members_display_name'],$seller['mgroup'])." (IP Address: ".$selling['ip_address'].")",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Title",
																			  $selling['title'],
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("Description<br />".$this->ipsclass->parser->pre_display_parse($selling['description']), 'left', 'tablerow1');
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Listed",
																			  $this->ipsclass->get_date($selling['time_selling'],'LONG'),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Trading",
																			  $this->ipsclass->numbers->format_currency($selling['selling_money']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Wanted",
																			  $this->ipsclass->numbers->format_currency($selling['want_money']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offer Type",
																			  $selling['trade_exact'] ? "At least" : "Anything",
																	 )      );
		
		$offers = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offers",
																			  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=offers&amp;sell=".$selling['t_id']."'>".$offers['total']."</a>",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"                         , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description"             , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category/Time Bought/Expires" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Price Bought/Resell"          , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items to trade");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item))),
																				  "Category: ".$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name']."<br />Time Bought:".$this->ipsclass->get_date($item['time_bought'],'LONG')."<br />Expires: ".$this->ipsclass->get_date($item['expired'],'LONG'),
																				  "Price Bought: ".$this->ipsclass->numbers->format_currency($item['price_bought'])."<br />Resell Price: ".$this->ipsclass->numbers->format_currency($item['resell_price']),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"             , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description" , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category"         , "" );
		$this->ipsclass->adskin->td_header[] = array( "Quanity"          , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items wanted back");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$this->ipsclass->input['sell']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_store_description())),
																				  $this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name'],
																				  $this->ipsclass->numbers->do_number_format($item['quanity']),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function acp_list_offers()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling)
		{
			$this->ipsclass->admin->error("Cannot find trade");
		}
		
		
		$this->ipsclass->admin->page_title  = "Welcome to offer list for Trade";
		$this->ipsclass->admin->page_detail = "Welcome to offer list for Trade";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'List Trades' );
		
		
		$this->ipsclass->adskin->td_header[] = array( "Offerer/IP Address"                     , "" );
		$this->ipsclass->adskin->td_header[] = array( "Offer Time/# of Items"                  , "" );
		$this->ipsclass->adskin->td_header[] = array( $this->ipsclass->vars['market_currency'] , "" );
		$this->ipsclass->adskin->td_header[] = array( "Options"                                , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Offers for ".$selling['title']);
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'o.*',
												'from' => array('market_trade_offers'=>'o'),
												'add_join' => array(
																		array(
																				'select' => 'm.mgroup, m.members_display_name',
																				'from' => array('members'=>'m'),
																				'where' => 'm.id=o.member_id',
																				'type' => 'left',
																			),
																		array(
																				'select' => 'COUNT(i.t_id) as items',
																				'from' => array('market_trade_offer_items'=>'i'),
																				'where' => 'i.member_id=o.member_id AND i.t_id=o.t_id',
																				'type' => 'left',
																			),
																	),
												'where' => "o.t_id=".$this->ipsclass->input['sell'],
												'group' => 'i.member_id',
												'order' => 'o.offer_time ASC',
										)	);
		$this->ipsclass->DB->exec_query();
		while($offer = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "Offerer: ".$this->ipsclass->market_class->format_name($offer['member_id'],$offer['members_display_name'],$offer['mgroup'])."<br />IP Address:".$offer['ip_address'],
																				  "Time: ".$this->ipsclass->get_date($offer['offer_time'],'LONG')."<br />Items: <a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=ioffers&amp;sell=".$offer['t_id']."&amp;member=".$offer['member_id']."'>".$this->ipsclass->numbers->do_number_format($offer['items'])."</a>",
																				  $this->ipsclass->numbers->format_currency($offer['offer_money']),
																				  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=roffers&amp;sell=".$offer['t_id']."&amp;member=".$offer['member_id']."'>Remove Offer</a>",
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function acp_list_items_offers()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		$this->ipsclass->input['member'] = intval($this->ipsclass->input['member']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling)
		{
			$this->ipsclass->admin->error("Cannot find trade");
		}
		
		$offer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->input['member']));
		
		if(!$offer)
		{
			$this->ipsclass->admin->error("Cannot find offer");
		}
		
		$seller = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$selling['member_id']));
		$offerer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$offer['member_id']));
		
		$this->ipsclass->admin->page_title  = "Welcome to trade details for Trade";
		$this->ipsclass->admin->page_detail = "Welcome to trade details for Trade";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'List Trades' );
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Offer Details");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offerer",
																			  $this->ipsclass->market_admin->format_name($offer['member_id'],$offerer['members_display_name'],$offerer['mgroup'])." (IP Address: ".$offer['ip_address'].")",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offer Time",
																			  $this->ipsclass->get_date($offer['offer_time'],'LONG'),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Offering",
																			  $this->ipsclass->numbers->format_currency($offer['offer_money']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"                         , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description"             , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category/Time Bought/Expires" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Price Bought/Resell"          , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items offered");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_offer_items','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->input['member']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item))),
																				  "Category: ".$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name']."<br />Time Bought:".$this->ipsclass->get_date($item['time_bought'],'LONG')."<br />Expires: ".$this->ipsclass->get_date($item['expired'],'LONG'),
																				  "Price Bought: ".$this->ipsclass->numbers->format_currency($item['price_bought'])."<br />Resell Price: ".$this->ipsclass->numbers->format_currency($item['resell_price']),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= "<hr />";
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Trade Details");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Seller",
																			  $this->ipsclass->market_admin->format_name($selling['member_id'],$seller['members_display_name'],$seller['mgroup'])." (IP Address: ".$selling['ip_address'].")",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Title",
																			  $selling['title'],
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("Description<br />".$this->ipsclass->parser->pre_display_parse($selling['description']), 'left', 'tablerow1');
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Listed",
																			  $this->ipsclass->get_date($selling['time_selling'],'LONG'),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Trading",
																			  $this->ipsclass->numbers->format_currency($selling['selling_money']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Wanted",
																			  $this->ipsclass->numbers->format_currency($selling['want_money']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offer Type",
																			  $selling['trade_exact'] ? "At least" : "Anything",
																	 )      );
		
		$offers = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offers",
																			  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=offers&amp;sell=".$selling['t_id']."'>".$offers['total']."</a>",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"                         , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description"             , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category/Time Bought/Expires" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Price Bought/Resell"          , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items to trade");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item))),
																				  "Category: ".$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name']."<br />Time Bought:".$this->ipsclass->get_date($item['time_bought'],'LONG')."<br />Expires: ".$this->ipsclass->get_date($item['expired'],'LONG'),
																				  "Price Bought: ".$this->ipsclass->numbers->format_currency($item['price_bought'])."<br />Resell Price: ".$this->ipsclass->numbers->format_currency($item['resell_price']),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"             , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description" , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category"         , "" );
		$this->ipsclass->adskin->td_header[] = array( "Quanity"          , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items wanted back");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$this->ipsclass->input['sell']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_store_description())),
																				  $this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name'],
																				  $this->ipsclass->numbers->do_number_format($item['quanity']),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function acp_remove_offer()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		$this->ipsclass->input['member'] = intval($this->ipsclass->input['member']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling)
		{
			$this->ipsclass->admin->error("Cannot find trade");
		}
		
		$offer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->input['member']));
		
		if(!$offer)
		{
			$this->ipsclass->admin->error("Cannot find offer");
		}
		
		$seller = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$selling['member_id']));
		$offerer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$offer['member_id']));
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$this->ipsclass->input['sell']));
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			$inv_item = array(
								'member_id'		=> $item['member_id'],
								'item'			=> $item['item'],
								'price_bought'	=> $item['price_bought'],
								'resell_price'	=> $item['resell_price'],
								'time_bought'	=> $item['time_bought'],
								'settings'		=> $item['settings'],
								'expired'		=> $item['expired'],
							);
			
			$this->ipsclass->DB->do_insert('market_inventory',$inv_item);
		}
		$this->ipsclass->DB->do_delete('market_trade_offer_items',"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->input['member']);
		
		$this->ipsclass->DB->do_update('members',array(
														$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($offerer[$this->ipsclass->vars['market_members_field']],$offer['offer_money']),
				),"id=".$this->ipsclass->input['member']);
		
		$this->ipsclass->DB->do_delete('market_trade_offers',"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->input['member']);
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=offers&sell=".$this->ipsclass->input['sell'],"Offer Removed...");
	}
	
	function acp_edit_trade()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling)
		{
			$this->ipsclass->admin->error("Cannot find trade");
		}
		
		$seller = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$selling['member_id']));
		
		$this->ipsclass->admin->page_title  = "Welcome to trade details for Trade";
		$this->ipsclass->admin->page_detail = "Welcome to trade details for Trade";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'List Trades' );
		
		$this->ipsclass->html .= $this->_acp_start_form(array(
																	array('action','save'),
																	array('sell',$this->ipsclass->input['sell']),
														)	);
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Trade Details");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Seller",
																			  $this->ipsclass->market_admin->format_name($selling['member_id'],$seller['members_display_name'],$seller['mgroup'])." (IP Address: ".$selling['ip_address'].")",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Title",
																			  $this->ipsclass->adskin->form_input("title", $selling['title']),
																	 )      );
		
		$description = $selling['description'];
		if ( $this->ipsclass->han_editor->method == 'rte' )
		{
			$description = $this->ipsclass->parser->convert_ipb_html_to_html( $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $description ) ) );
		}
		else
		{
			$description = $this->ipsclass->parser->pre_edit_parse( $description );
		}
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("Description<br />".$this->ipsclass->han_editor->show_editor( $description, 'description' ), 'left', 'tablerow1');
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Listed",
																			  $this->ipsclass->get_date($selling['time_selling'],'LONG'),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Trading",
																			  $this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("selling_money",$selling['selling_money'])),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Wanted",
																			  $this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("want_money",$selling['want_money'])),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offer Type",
																			  $this->ipsclass->adskin->form_dropdown("trade_exact", array(array(0,'Anything'), array(1,'At least')),$selling['trade_exact']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"                         , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description"             , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category/Time Bought/Expires" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Price Bought/Resell"          , "" );
		$this->ipsclass->adskin->td_header[] = array( "Remove?"                      , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items to trade");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item))),
																				  "Category: ".$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name']."<br />Time Bought:".$this->ipsclass->get_date($item['time_bought'],'LONG')."<br />Expires: ".$this->ipsclass->get_date($item['expired'],'LONG'),
																				  "Price Bought: ".$this->ipsclass->numbers->format_currency($item['price_bought'])."<br />Resell Price: ".$this->ipsclass->numbers->format_currency($item['resell_price']),
																				  $this->ipsclass->adskin->form_checkbox("item[".$item['i_id']."]"),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"             , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description" , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category"         , "" );
		$this->ipsclass->adskin->td_header[] = array( "Quanity"          , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items wanted back");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$this->ipsclass->input['sell']));
		$this->ipsclass->DB->exec_query();
		$wants = array();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$wants[$item['item']] = $item['quanity'];
		}
		foreach($this->ipsclass->cache['market_items'] as $item)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$item['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$item['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item_id']]->parse_store_description())),
																				  $this->ipsclass->cache['market_category'][$item['category']]['cat_name'],
																				  $this->ipsclass->adskin->form_simple_input("want[".$item['item_id']."]", $wants[$item['item_id']] ? $wants[$item['item_id']] : 0),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form_standalone("Save");
		
		$this->ipsclass->admin->output();
	}
	
	function acp_save_trade()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling)
		{
			$this->ipsclass->admin->error("Cannot find trade");
		}
		
		$seller = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$selling['member_id']));
		
		$wanted = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$selling['t_id']));
		$this->ipsclass->DB->exec_query();
		while($want = $this->ipsclass->DB->fetch_row())
		{
			$wanted[$want['item']] = $want;
		}
		
		$this->ipsclass->input['title'] = $this->ipsclass->parser->bad_words($this->ipsclass->input['title']);
		$this->ipsclass->input['selling_money'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['selling_money'],'int');
		$this->ipsclass->input['selling_money'] = $this->ipsclass->input['selling_money'] > 0 ? $this->ipsclass->input['selling_money'] : 0;
		$this->ipsclass->input['want_money'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['want_money'],'int');
		$this->ipsclass->input['want_money'] = $this->ipsclass->input['want_money'] > 0 ? $this->ipsclass->input['want_money'] : 0;
		$this->ipsclass->input['trade_exact'] = intval($this->ipsclass->input['trade_exact']) == 0 ? 0 : 1;
		$description = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->han_editor->process_raw_post( 'description' ) ) );
		
		if($this->ipsclass->numbers->compare($this->ipsclass->input['selling_money'],$selling['selling_money']) != 0 && $this->ipsclass->numbers->compare($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->numbers->subtract($this->ipsclass->input['selling_money'],$selling['selling_money'])) < 0)
		{
			$this->ipsclass->Error(array('MSG'=>'trade_not_enough_money'));
		}
		
		$this->ipsclass->DB->do_update('market_trade_selling',array(
																		'title' => $this->ipsclass->input['title'],
																		'description' => $description,
																		'trade_exact' => $this->ipsclass->input['trade_exact'],
																		'selling_money' => $this->ipsclass->input['selling_money'],
																		'want_money' => $this->ipsclass->input['want_money'],
									),"t_id=".$this->ipsclass->input['sell']);
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if($this->ipsclass->input['item'][$item['i_id']])
			{
				$this->ipsclass->DB->do_insert('market_inventory',array(
																			'member_id' => $item['member_id'],
																			'item' => $item['item'],
																			'price_bought' => $item['price_bought'],
																			'resell_price' => $item['resell_price'],
																			'time_bought' => $item['time_bought'],
																			'settings' => $item['settings'],
																			'expired' => $item['expired'],
												)						);
				$this->ipsclass->DB->do_delete('market_trade_selling_items',"i_id=".$item['i_id']);
			}
		}
		
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			if(in_array($item_id,$this->settings['want_items']))
			{
				$this->ipsclass->input['want'][$item_id] = intval($this->ipsclass->input['want'][$item_id]);
				if($this->ipsclass->input['want'][$item_id])
				{
					if(isset($wanted[$item_id]))
					{
						$this->ipsclass->DB->do_update('market_trade_selling_wants',array(
																							'quanity' => $this->ipsclass->input['want'][$item_id],
							),"t_id=".$this->ipsclass->input['sell']." AND item=".$item_id);
					}
					else
					{
						$this->ipsclass->DB->do_insert('market_trade_selling_wants',array(
																							't_id' => $this->ipsclass->input['sell'],
																							'item' => $item_id,
																							'quanity' => $this->ipsclass->input['want'][$item_id],
														)								);
					}
				}
				else
				{
					if(isset($wanted[$item_id]))
					{
						$this->ipsclass->DB->do_delete('market_trade_selling_wants',"t_id=".$this->ipsclass->input['sell']." AND item=".$item_id);
					}
				}
			}
		}
		
		if($this->ipsclass->numbers->compare($this->ipsclass->input['selling_money'],$selling['selling_money']) != 0)
		{
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($seller[$this->ipsclass->vars['market_members_field']],$this->ipsclass->numbers->subtract($this->ipsclass->input['selling_money'],$selling['selling_money'])),
							),"id=".$selling['member_id']);
		}
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=detail&sell=".$this->ipsclass->input['sell'],"Trade Information saved...");
	}
	
	function acp_remove_trade()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling)
		{
			$this->ipsclass->admin->error("Cannot find trade");
		}
		
		$seller = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$selling['member_id']));
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_offer_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_insert('market_inventory',array(
																		'member_id' => $item['member_id'],
																		'item' => $item['item'],
																		'price_bought' => $item['price_bought'],
																		'resell_price' => $item['resell_price'],
																		'time_bought' => $item['time_bought'],
																		'settings' => $item['settings'],
																		'expired' => $item['expired'],
											)						);
		}
		$this->ipsclass->DB->do_delete('market_trade_offer_items',"t_id=".$this->ipsclass->input['sell'] );
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_insert('market_inventory',array(
																		'member_id' => $this->ipsclass->member['id'],
																		'item' => $item['item'],
																		'price_bought' => $item['price_bought'],
																		'resell_price' => $item['resell_price'],
																		'time_bought' => $item['time_bought'],
																		'settings' => $item['settings'],
																		'expired' => $item['expired'],
											)						);
		}
		$this->ipsclass->DB->do_delete('market_trade_selling_items',"t_id=".$this->ipsclass->input['sell'] );
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'o.*',
												'from' => array('market_trade_offers'=>'o'),
												'add_join' => array(
																		array(
																				'select' => 'm.'.$this->ipsclass->vars['market_members_field'],
																				'from' => array('members'=>'m'),
																				'where' => 'm.id=o.member_id',
																				'type' => 'left',
																			),
																	),
												'where' => "o.t_id=".$this->ipsclass->input['sell'],
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($offer = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($offer[$this->ipsclass->vars['market_members_field']],$offer['offer_money']),
								),"id=".$offer['member_id']);
		}
		$this->ipsclass->DB->do_delete('market_trade_offers',"t_id=".$this->ipsclass->input['sell'] );
		
		$this->ipsclass->DB->do_delete('market_trade_selling_wants',"t_id=".$this->ipsclass->input['sell']);
		
		$this->ipsclass->DB->do_update('members',array(
														$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($seller[$this->ipsclass->vars['market_members_field']],$selling['selling_money']),
				),"id=".$this->ipsclass->member['id']);
		$this->ipsclass->DB->do_delete('market_trade_selling',"t_id=".$this->ipsclass->input['sell']);
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js,"Trade removed...");
	}
	
	function acp_list_previous()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		$LIMIT = 50;
		
		$this->ipsclass->admin->page_title  = "Welcome to trades list for Trade";
		$this->ipsclass->admin->page_detail = "Welcome to trades list for Trade";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'List Trades' );
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_trade_logs'));
		$page = $this->ipsclass->adskin->build_pagelinks(array(	'TOTAL_POSS'   => $total['total'],
																'PER_PAGE'     => $LIMIT,
																'USE_ST'       => 'st',
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => "Single Page",
																'BASE_URL'     => $this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=previous"
														)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->html .= "<br />";
		
		$this->ipsclass->adskin->td_header[] = array( "Name/Starting/Sold" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Seller/IP Address"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "# of Items/Offers"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "Want/Trade Prices"  , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Completed Trade Listing");
		
		$this->ipsclass->DB->build_query(array(
												'select' => 's.*',
												'from' => array('market_trade_logs'=>'s'),
												'add_join' => array(
																		array(
																				'select' => 'COUNT(i.t_id) as items',
																				'from' => array('market_trade_log_items'=>'i'),
																				'where' => 'i.t_id=s.t_id',
																				'type' => 'left'
																			),
																		array(
																				'select' => 'm.members_display_name, m.mgroup',
																				'from' => array('members'=>'m'),
																				'where' => 'm.id=s.member_id',
																				'type' => 'left'
																			),
																	),
												'order' => 's.time_selling ASC',
												'group' => 'i.t_id',
												'limit' => array($this->ipsclass->input['st'],$LIMIT),
										)	);
		$q = $this->ipsclass->DB->exec_query();
		$sell = array();
		while($selling = $this->ipsclass->DB->fetch_row($q))
		{
			$selling['offers'] = 0;
			$selling['items'] = $this->ipsclass->do_number_format($selling['items']);
			$selling['selling_money'] = $this->ipsclass->numbers->format_currency($selling['selling_money']);
			$selling['want_money'] = $this->ipsclass->numbers->format_currency($selling['want_money']);
			$selling['member_name'] = $this->ipsclass->market_class->format_name($selling['member_id'],$selling['members_display_name'],$selling['mgroup']);
			$selling['time_selling'] = $this->ipsclass->get_date($selling['time_selling'],'LONG');
			$selling['time_sold'] = $this->ipsclass->get_date($selling['time_sold'],'LONG');
			$sell[$selling['t_id']] = $selling;
		}
		if(count($sell) == 0)
		{	
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No active trades",'center');
		}
		else
		{
			$this->ipsclass->DB->build_query(array('select'=>'COUNT(*) as offers, t_id','from'=>'market_trade_offers','where'=>"t_id IN (".implode(", ",array_keys($sell)).")",'group'=>'t_id'));
			$this->ipsclass->DB->exec_query();
			while($offers = $this->ipsclass->DB->fetch_row())
			{
				$sell[$offers['t_id']]['offers'] = $this->ipsclass->do_number_format($offers['offers']);
			}
		}
		
		foreach($sell as $selling)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=cdetail&amp;sell=".$selling['t_id']."'><b>".$selling['title']."</b></a><br />Starting: ".$selling['time_selling']."<br />Sold: ".$selling['time_sold'],
																				  "Seller: ".$this->ipsclass->market_admin->format_name($selling['member_id'],$selling['members_display_name'],$selling['mgroup'])."<br />IP Address: ".$selling['ip_address'],
																				  "Items: <a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=cdetail&amp;sell=".$selling['t_id']."'>".$selling['items']."</a><br />Offers: <a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=coffers&amp;sell=".$selling['t_id']."'>".$selling['offers']."</a>",
																				  "Trade ".$this->ipsclass->vars['market_currency'].": ".$selling['selling_money']."<br />Wanted ".$this->ipsclass->vars['market_currency'].": ".$selling['want_money'],
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->admin->output();
	}
	
	function acp_completed_trade_details()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_logs','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling)
		{
			$this->ipsclass->admin->error("Cannot find trade");
		}
		
		$seller = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$selling['member_id']));
		
		$this->ipsclass->admin->page_title  = "Welcome to compeleted trade details for Trade";
		$this->ipsclass->admin->page_detail = "Welcome to compeleted trade details for Trade";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'List Trades' );
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Completed Trade Details");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Seller",
																			  $this->ipsclass->market_admin->format_name($selling['member_id'],$seller['members_display_name'],$seller['mgroup'])." (IP Address: ".$selling['ip_address'].")",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Title",
																			  $selling['title'],
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("Description<br />".$this->ipsclass->parser->pre_display_parse($selling['description']), 'left', 'tablerow1');
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Listed",
																			  $this->ipsclass->get_date($selling['time_selling'],'LONG'),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Completed",
																			  $this->ipsclass->get_date($selling['time_sold'],'LONG'),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Trading",
																			  $this->ipsclass->numbers->format_currency($selling['selling_money']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Wanted",
																			  $this->ipsclass->numbers->format_currency($selling['want_money']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offer Type",
																			  $selling['trade_exact'] ? "At least" : "Anything",
																	 )      );
		
		$offers = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offers",
																			  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=coffers&amp;sell=".$selling['t_id']."'>".$offers['total']."</a>",
																	 )      );
		
		$offer_picked = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']." AND picked=1"));
		$offerer_picked = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$offer_picked['member_id']));
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offer Picked",
																			  $this->ipsclass->market_admin->format_name($offerer_picked['id'],$offerer_picked['members_display_name'],$offerer_picked['mgroup']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"             , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description" , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category"         , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items to trade");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_log_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_store_description())),
																				  "Category: ".$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name'],
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"             , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description" , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category"         , "" );
		$this->ipsclass->adskin->td_header[] = array( "Quanity"          , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items wanted back");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_log_wants','where'=>"t_id=".$this->ipsclass->input['sell']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_store_description())),
																				  $this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name'],
																				  $this->ipsclass->numbers->do_number_format($item['quanity']),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function acp_completed_list_offers()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_logs','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling)
		{
			$this->ipsclass->admin->error("Cannot find trade");
		}
		
		
		$this->ipsclass->admin->page_title  = "Welcome to offer list for Trade";
		$this->ipsclass->admin->page_detail = "Welcome to offer list for Trade";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'List Trades' );
		
		
		$this->ipsclass->adskin->td_header[] = array( "Offerer/IP Address"                     , "" );
		$this->ipsclass->adskin->td_header[] = array( "Offer Time/# of Items"                  , "" );
		$this->ipsclass->adskin->td_header[] = array( $this->ipsclass->vars['market_currency'] , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Offers for ".$selling['title']);
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'o.*',
												'from' => array('market_trade_offers'=>'o'),
												'add_join' => array(
																		array(
																				'select' => 'm.mgroup, m.members_display_name',
																				'from' => array('members'=>'m'),
																				'where' => 'm.id=o.member_id',
																				'type' => 'left',
																			),
																		array(
																				'select' => 'COUNT(i.t_id) as items',
																				'from' => array('market_trade_offer_items'=>'i'),
																				'where' => 'i.member_id=o.member_id AND i.t_id=o.t_id',
																				'type' => 'left',
																			),
																	),
												'where' => "o.t_id=".$this->ipsclass->input['sell'],
												'group' => 'i.member_id',
												'order' => 'o.picked DESC, o.offer_time ASC',
										)	);
		$this->ipsclass->DB->exec_query();
		while($offer = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "Offerer: ".$this->ipsclass->market_class->format_name($offer['member_id'],$offer['members_display_name'],$offer['mgroup']).($offer['picked'] > 0 ? " <font style='font-style: italic'>Picked</font>" : "")."<br />IP Address:".$offer['ip_address'],
																				  "Time: ".$this->ipsclass->get_date($offer['offer_time'],'LONG')."<br />Items: <a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=cioffers&amp;sell=".$offer['t_id']."&amp;member=".$offer['member_id']."'>".$this->ipsclass->numbers->do_number_format($offer['items'])."</a>",
																				  $this->ipsclass->numbers->format_currency($offer['offer_money']),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function acp_completed_list_items_offers()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		$this->ipsclass->input['member'] = intval($this->ipsclass->input['member']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_logs','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling)
		{
			$this->ipsclass->admin->error("Cannot find trade");
		}
		
		$offer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->input['member']));
		
		if(!$offer)
		{
			$this->ipsclass->admin->error("Cannot find offer");
		}
		
		$seller = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$selling['member_id']));
		$offerer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$offer['member_id']));
		
		$this->ipsclass->admin->page_title  = "Welcome to trade details for Trade";
		$this->ipsclass->admin->page_detail = "Welcome to trade details for Trade";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'List Trades' );
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Offer Details");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offerer",
																			  $this->ipsclass->market_admin->format_name($offer['member_id'],$offerer['members_display_name'],$offerer['mgroup'])." (IP Address: ".$offer['ip_address'].")",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offer Time",
																			  $this->ipsclass->get_date($offer['offer_time'],'LONG'),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Offering",
																			  $this->ipsclass->numbers->format_currency($offer['offer_money']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"                         , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description"             , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category/Time Bought/Expires" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Price Bought/Resell"          , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items offered");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_offer_items','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->input['member']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item))),
																				  "Category: ".$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name']."<br />Time Bought:".$this->ipsclass->get_date($item['time_bought'],'LONG')."<br />Expires: ".$this->ipsclass->get_date($item['expired'],'LONG'),
																				  "Price Bought: ".$this->ipsclass->numbers->format_currency($item['price_bought'])."<br />Resell Price: ".$this->ipsclass->numbers->format_currency($item['resell_price']),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= "<hr />";
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Trade Details");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Seller",
																			  $this->ipsclass->market_admin->format_name($selling['member_id'],$seller['members_display_name'],$seller['mgroup'])." (IP Address: ".$selling['ip_address'].")",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Title",
																			  $selling['title'],
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("Description<br />".$this->ipsclass->parser->pre_display_parse($selling['description']), 'left', 'tablerow1');
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Listed",
																			  $this->ipsclass->get_date($selling['time_selling'],'LONG'),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Completed",
																			  $this->ipsclass->get_date($selling['time_sold'],'LONG'),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Trading",
																			  $this->ipsclass->numbers->format_currency($selling['selling_money']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  $this->ipsclass->vars['market_currency']." Wanted",
																			  $this->ipsclass->numbers->format_currency($selling['want_money']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offer Type",
																			  $selling['trade_exact'] ? "At least" : "Anything",
																	 )      );
		
		$offers = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offers",
																			  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=coffers&amp;sell=".$selling['t_id']."'>".$offers['total']."</a>",
																	 )      );
		
		$offer_picked = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']." AND picked=1"));
		$offerer_picked = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$offer_picked['member_id']));
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Offer Picked",
																			  $this->ipsclass->market_admin->format_name($offerer_picked['id'],$offerer_picked['members_display_name'],$offerer_picked['mgroup']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"                         , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description"             , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category/Time Bought/Expires" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Price Bought/Resell"          , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items to trade");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item))),
																				  "Category: ".$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name']."<br />Time Bought:".$this->ipsclass->get_date($item['time_bought'],'LONG')."<br />Expires: ".$this->ipsclass->get_date($item['expired'],'LONG'),
																				  "Price Bought: ".$this->ipsclass->numbers->format_currency($item['price_bought'])."<br />Resell Price: ".$this->ipsclass->numbers->format_currency($item['resell_price']),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"             , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description" , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "Category"         , "" );
		$this->ipsclass->adskin->td_header[] = array( "Quanity"          , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Items wanted back");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$this->ipsclass->input['sell']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_store_description())),
																				  $this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name'],
																				  $this->ipsclass->numbers->do_number_format($item['quanity']),
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function run()
	{
		$this->ipsclass->load_template('skin_market_plugin');
		$this->ipsclass->load_language('lang_market_plugin');
		
		$this->ipsclass->market_class->title[] = array($this->plugin['name'],'market_'.$this->plugin['link']);
		if(in_array($this->ipsclass->member['mgroup'],$this->plugin['use_permission']))
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->ipsclass->lang['trade_list_selling']."</a></b>",
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=offer_manage'>".$this->ipsclass->lang['trade_manage_offers']."</a></b>",
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=manage'>".$this->ipsclass->lang['trade_manage_selling']."</a></b>",
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=sold'>".$this->ipsclass->lang['trade_list_sold']."</a></b>",
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=sell'>".$this->ipsclass->lang['trade_make_a_sell']."</a></b>",
														);
			
			$do_redirect = array(
									'list' => 'list_selling',
									'selling' => 'view_selling',
									'manage' => 'manage_selling',
									'sold' => 'list_sold',
									'offers' => 'list_offers',
									'pick' => 'pick_offer',
									'sell' => 'make_trade',
									'add_sell' => 'create_trade',
									'edit_sell' => 'edit_trade',
									'save_sell' => 'save_trade',
									'offer' => 'make_offer',
									'do_offer' => 'create_offer',
									'sold_detail' => 'view_sold',
									'sold_offer' => 'sold_offers',
									'offer_manage' => 'manage_offers',
									'remove_offer' => 'remove_offer',
									'delete_sell' => 'delete_trade',
								  );
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'list';
			}
		}
		else
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->ipsclass->lang['trade_list_selling']."</a></b>",
														);
			
			$do_redirect = array(
									'list' => 'list_selling',
									'selling' => 'view_selling',
								);
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'list';
			}
		}
		
		$func = 'run_'.$do_redirect[$this->ipsclass->input['do']];
		$this->$func();
	}
	
	function run_list_selling()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		$LIMIT = 50;
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_trade_selling'));
		$page_links = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																'PER_PAGE'     => $LIMIT,
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=list",0,'st'),
																'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=list",
																'USE_ST'       => 'st'
														)	);
		
		$selling = array();
		$this->ipsclass->DB->build_query(array(
												'select' => 's.*',
												'from' => array('market_trade_selling'=>'s'),
												'add_join' => array(
																		array(
																				'select' => 'COUNT(si.i_id) no_items',
																				'from' => array('market_trade_selling_items' => 'si'),
																				'where' => 's.t_id=si.t_id',
																				'type' => 'left',
																			),
																		array(
																				'select' => 'm.members_display_name,m.mgroup',
																				'from' => array('members' => 'm'),
																				'where' => 'm.id=si.member_id',
																				'type' => 'left',
																			),
																	),
												'group' => 's.t_id',
												'limit' => array($this->ipsclass->input['st'],$LIMIT)
										)	);
		$this->ipsclass->DB->exec_query();
		while($sell = $this->ipsclass->DB->fetch_row())
		{
			$sell['no_items'] = $this->ipsclass->do_number_format($sell['no_items']);
			$sell['time_selling'] = $this->ipsclass->get_date($sell['time_selling'],'LONG');
			$sell['member_name'] = $this->ipsclass->market_class->format_name($sell['member_id'],$sell['members_display_name'],$sell['mgroup']);
			$selling[] = $sell;
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_list_selling($selling, $page_links);
	}
	
	function run_view_selling()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		$selling = $this->ipsclass->DB->build_and_exec_query(array(
																	'select' => 's.*',
																	'from' => array('market_trade_selling'=>'s'),
																	'add_join' => array(
																							array(
																									'select' => 'm.members_display_name,m.mgroup',
																									'from' => array('members' => 'm'),
																									'where' => 'm.id=s.member_id',
																									'type' => 'left',
																								),
																						),
																	'where' => "s.t_id=".$this->ipsclass->input['sell'],
															)	);
		
		if(!$selling)
		{
			$this->ipsclass->Error(array('MSG'=>'missing_files'));
		}
		
		if($selling['member_id'] != $this->ipsclass->member['id'])
		{
			$offer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->member['id']));
		}
		else
		{
			$offer = false;
		}
		
		$selling['member_name'] = $this->ipsclass->market_class->format_name($selling['member_id'],$selling['members_display_name'],$selling['mgroup']);
		$selling['time_selling'] = $this->ipsclass->get_date($selling['time_selling'],'LONG');
		$selling['selling_money'] = $this->ipsclass->numbers->format_currency($selling['selling_money']);
		$selling['want_money'] = $this->ipsclass->numbers->format_currency($selling['want_money']);
		$selling['trade_exact'] = $selling['trade_exact'] ? $this->ipsclass->lang['trade_offer_match'] : $this->ipsclass->lang['trade_offer_anything'];
		$selling['description'] = $this->ipsclass->parser->pre_display_parse($selling['description']);
		
		$items = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_items','where'=>"t_id=".$selling['t_id']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
			$item['expired'] = $item['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($item['expired'],'TINY');
			$items[] = $item;
		}
		
		$selling['no_items'] = count($items);
		
		$wanted = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$selling['t_id']));
		$this->ipsclass->DB->exec_query();
		while($want = $this->ipsclass->DB->fetch_row())
		{
			$want['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$want['item']]->parse_store_description()));
			$want['quanity'] = $this->ipsclass->do_number_format($want['quanity']);
			$wanted[] = $want;
		}
		
		$this->ipsclass->lang['trade_money_trading'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_money_trading']);
		$this->ipsclass->lang['trade_money_want'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_money_want']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_selling_full_page($selling, $items, $wanted, $offer);
	}
	
	function run_make_trade()
	{
		foreach($this->ipsclass->market_class->inventory_id as $inv_id => $item)
		{
			$this->ipsclass->market_class->inventory_id[$inv_id]['time_bought'] = $this->ipsclass->get_date($item['time_bought'],'TINY');
			$this->ipsclass->market_class->inventory_id[$inv_id]['fprice_bought'] = $item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price_bought']);
			$this->ipsclass->market_class->inventory_id[$inv_id]['fresell_price'] = $item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item['resell_price']);
			$this->ipsclass->market_class->inventory_id[$inv_id]['checker'] = $item['i_id'];
			$this->ipsclass->market_class->inventory_id[$inv_id]['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
			$this->ipsclass->market_class->inventory_id[$inv_id]['expired'] = $item['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($item['expired'],'TINY');
		}
		
		$items = array();
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			if(!in_array($item_id,$this->settings['want_items']))
			{
				continue;
			}
			$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item_id]->parse_store_description($item)));
			$item['fprice'] = $item['price'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price']);
			$items[$item_id] = $item;
		}
		
		$this->ipsclass->han_editor->remove_side_panel = 1;
		$editor = $this->ipsclass->han_editor->show_editor( '', 'description' );
		
		$this->ipsclass->lang['trade_money_to_sell'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_money_to_sell']);
		$this->ipsclass->lang['trade_money_wanted'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_money_wanted']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_make_trade_page($items, $editor);
	}
	
	function run_create_trade()
	{
		$this->ipsclass->input['title'] = $this->ipsclass->parser->bad_words($this->ipsclass->input['title']);
		$this->ipsclass->input['selling_money'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['selling_money'],'int');
		$this->ipsclass->input['selling_money'] = $this->ipsclass->input['selling_money'] > 0 ? $this->ipsclass->input['selling_money'] : 0;
		$this->ipsclass->input['want_money'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['want_money'],'int');
		$this->ipsclass->input['want_money'] = $this->ipsclass->input['want_money'] > 0 ? $this->ipsclass->input['want_money'] : 0;
		$this->ipsclass->input['trade_exact'] = intval($this->ipsclass->input['trade_exact']) == 0 ? 0 : 1;
		$description = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->han_editor->process_raw_post( 'description' ) ) );
		
		if($this->ipsclass->numbers->compare($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->input['selling_money']) < 0)
		{
			$this->ipsclass->Error(array('MSG'=>'trade_not_enough_money'));
		}
		
		$this->ipsclass->DB->do_insert('market_trade_selling',array(
																		'member_id' => $this->ipsclass->member['id'],
																		'title' => $this->ipsclass->input['title'],
																		'description' => $description,
																		'time_selling' => time(),
																		'trade_exact' => $this->ipsclass->input['trade_exact'],
																		'selling_money' => $this->ipsclass->input['selling_money'],
																		'want_money' => $this->ipsclass->input['want_money'],
																		'ip_address' => $this->ipsclass->ip_address,
									)								);
		$selling_id = $this->ipsclass->DB->get_insert_id();
		
		foreach($this->ipsclass->market_class->inventory_id as $inv_id => $item)
		{
			if(isset($this->ipsclass->input['inv_id'][$inv_id]) && $this->ipsclass->input['inv_id'][$inv_id] == 1)
			{
				$this->ipsclass->DB->do_insert('market_trade_selling_items',array(
																					't_id' => $selling_id,
																					'member_id' => $item['member_id'],
																					'item' => $item['item'],
																					'price_bought' => $item['price_bought'],
																					'resell_price' => $item['resell_price'],
																					'time_bought' => $item['time_bought'],
																					'settings' => $item['settings'],
																					'expired' => $item['expired'],
												)								);
				$this->ipsclass->DB->do_delete('market_inventory',"i_id=".$inv_id);
			}
		}
		
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			if(in_array($item_id,$this->settings['want_items']))
			{
				$this->ipsclass->input['item_id'][$item_id] = intval($this->ipsclass->input['item_id'][$item_id]);
				if($this->ipsclass->input['item_id'][$item_id])
				{
					$this->ipsclass->DB->do_insert('market_trade_selling_wants',array(
																						't_id' => $selling_id,
																						'item' => $item_id,
																						'quanity' => $this->ipsclass->input['item_id'][$item_id],
													)								);
				}
			}
		}
		
		if($this->ipsclass->input['selling_money'])
		{
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->input['selling_money']),
					),"id=".$this->ipsclass->member['id']);
		}
		
		$this->ipsclass->market_class->redirect('trade_selling_added',"plugin=".$this->ipsclass->input['plugin']."&do=selling&sell=".$selling_id);
	}
	
	function run_edit_trade()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling || $selling['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$wanted = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$selling['t_id']));
		$this->ipsclass->DB->exec_query();
		while($want = $this->ipsclass->DB->fetch_row())
		{
			$wanted[$want['item']] = $want;
		}
		
		foreach($this->ipsclass->market_class->inventory_id as $inv_id => $item)
		{
			$this->ipsclass->market_class->inventory_id[$inv_id]['time_bought'] = $this->ipsclass->get_date($item['time_bought'],'TINY');
			$this->ipsclass->market_class->inventory_id[$inv_id]['fprice_bought'] = $item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price_bought']);
			$this->ipsclass->market_class->inventory_id[$inv_id]['fresell_price'] = $item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item['resell_price']);
			$this->ipsclass->market_class->inventory_id[$inv_id]['checker'] = $item['i_id'];
			$this->ipsclass->market_class->inventory_id[$inv_id]['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
			$this->ipsclass->market_class->inventory_id[$inv_id]['expired'] = $item['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($item['expired'],'TINY');
		}
		
		$items = array();
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			if(!in_array($item_id,$this->settings['want_items']))
			{
				continue;
			}
			$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item_id]->parse_store_description($item)));
			$item['fprice'] = $item['price'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price']);
			$item['quanity'] = isset($wanted[$item_id]) ? $wanted[$item_id]['quanity'] : 0;
			$items[$item_id] = $item;
		}
		
		$this->ipsclass->han_editor->remove_side_panel = 1;
		if ( $this->ipsclass->han_editor->method == 'rte' )
		{
			$selling['description'] = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $selling['description'] ) );
			$selling['description'] = $this->ipsclass->parser->convert_ipb_html_to_html( $selling['description'] );
		}
		else
		{
			$selling['description'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $selling['description'] ) );
		}
		$editor = $this->ipsclass->han_editor->show_editor( $selling['description'], 'description' );
		
		$this->ipsclass->lang['trade_money_to_sell'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_money_to_sell']);
		$this->ipsclass->lang['trade_money_wanted'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_money_wanted']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_edit_trade_page($selling, $items, $editor);
	}
	
	function run_save_trade()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling || $selling['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$wanted = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$selling['t_id']));
		$this->ipsclass->DB->exec_query();
		while($want = $this->ipsclass->DB->fetch_row())
		{
			$wanted[$want['item']] = $want;
		}
		
		$this->ipsclass->input['title'] = $this->ipsclass->parser->bad_words($this->ipsclass->input['title']);
		$this->ipsclass->input['selling_money'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['selling_money'],'int');
		$this->ipsclass->input['selling_money'] = $this->ipsclass->input['selling_money'] > 0 ? $this->ipsclass->input['selling_money'] : 0;
		$this->ipsclass->input['want_money'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['want_money'],'int');
		$this->ipsclass->input['want_money'] = $this->ipsclass->input['want_money'] > 0 ? $this->ipsclass->input['want_money'] : 0;
		$this->ipsclass->input['trade_exact'] = intval($this->ipsclass->input['trade_exact']) == 0 ? 0 : 1;
		$description = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->han_editor->process_raw_post( 'description' ) ) );
		
		if($this->ipsclass->numbers->compare($this->ipsclass->input['selling_money'],$selling['selling_money']) != 0 && $this->ipsclass->numbers->compare($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->numbers->subtract($this->ipsclass->input['selling_money'],$selling['selling_money'])) < 0)
		{
			$this->ipsclass->Error(array('MSG'=>'trade_not_enough_money'));
		}
		
		$this->ipsclass->DB->do_update('market_trade_selling',array(
																		'member_id' => $this->ipsclass->member['id'],
																		'title' => $this->ipsclass->input['title'],
																		'description' => $description,
																		'trade_exact' => $this->ipsclass->input['trade_exact'],
																		'selling_money' => $this->ipsclass->input['selling_money'],
																		'want_money' => $this->ipsclass->input['want_money'],
																		'ip_address' => $this->ipsclass->ip_address,
									),"t_id=".$this->ipsclass->input['sell']);
		
		foreach($this->ipsclass->market_class->inventory_id as $inv_id => $item)
		{
			if(isset($this->ipsclass->input['inv_id'][$inv_id]) && $this->ipsclass->input['inv_id'][$inv_id] == 1)
			{
				$this->ipsclass->DB->do_insert('market_trade_selling_items',array(
																					't_id' => $this->ipsclass->input['sell'],
																					'member_id' => $item['member_id'],
																					'item' => $item['item'],
																					'price_bought' => $item['price_bought'],
																					'resell_price' => $item['resell_price'],
																					'time_bought' => $item['time_bought'],
																					'settings' => $item['settings'],
																					'expired' => $item['expired'],
												)								);
				$this->ipsclass->DB->do_delete('market_inventory',"i_id=".$inv_id);
			}
		}
		
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			if(in_array($item_id,$this->settings['want_items']))
			{
				$this->ipsclass->input['item_id'][$item_id] = intval($this->ipsclass->input['item_id'][$item_id]);
				if($this->ipsclass->input['item_id'][$item_id])
				{
					if(isset($wanted[$item_id]))
					{
						$this->ipsclass->DB->do_update('market_trade_selling_wants',array(
																							'quanity' => $this->ipsclass->input['item_id'][$item_id],
							),"t_id=".$this->ipsclass->input['sell']." AND item=".$item_id);
					}
					else
					{
						$this->ipsclass->DB->do_insert('market_trade_selling_wants',array(
																							't_id' => $this->ipsclass->input['sell'],
																							'item' => $item_id,
																							'quanity' => $this->ipsclass->input['item_id'][$item_id],
														)								);
					}
				}
				else
				{
					if(isset($wanted[$item_id]))
					{
						$this->ipsclass->DB->do_delete('market_trade_selling_wants',"t_id=".$this->ipsclass->input['sell']." AND item=".$item_id);
					}
				}
			}
		}
		
		if($this->ipsclass->numbers->compare($this->ipsclass->input['selling_money'],$selling['selling_money']) != 0)
		{
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->numbers->subtract($this->ipsclass->input['selling_money'],$selling['selling_money'])),
					),"id=".$this->ipsclass->member['id']);
		}
		
		$this->ipsclass->market_class->redirect('trade_selling_saved',"plugin=".$this->ipsclass->input['plugin']."&do=selling&sell=".$this->ipsclass->input['sell']);
	}
	
	function run_make_offer()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling || $selling['member_id'] == $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$offer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->member['id']));
		if($offer)
		{
			$this->ipsclass->Error(array('MSG'=>'trade_already_made_offer'));
		}
				
		foreach($this->ipsclass->market_class->inventory_id as $inv_id => $item)
		{
			$this->ipsclass->market_class->inventory_id[$inv_id]['time_bought'] = $this->ipsclass->get_date($item['time_bought'],'TINY');
			$this->ipsclass->market_class->inventory_id[$inv_id]['fprice_bought'] = $item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price_bought']);
			$this->ipsclass->market_class->inventory_id[$inv_id]['fresell_price'] = $item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item['resell_price']);
			$this->ipsclass->market_class->inventory_id[$inv_id]['checker'] = $item['i_id'];
			$this->ipsclass->market_class->inventory_id[$inv_id]['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
			$this->ipsclass->market_class->inventory_id[$inv_id]['expired'] = $item['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($item['expired'],'TINY');
		}
		
		$this->ipsclass->lang['trade_money_offer'] = sprintf($this->ipsclass->lang['trade_money_offer'], $this->ipsclass->vars['market_currency']);
		$this->ipsclass->lang['trade_money_details'] = sprintf($this->ipsclass->lang['trade_money_details'], $this->ipsclass->vars['market_currency']);
		$this->ipsclass->lang['trade_money_offer_details'] = sprintf($this->ipsclass->lang['trade_money_offer_details'], $this->ipsclass->vars['market_currency']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_make_offer_page($selling);
	}
	
	function run_create_offer()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling || $selling['member_id'] == $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$offer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->member['id']));
		if($offer)
		{
			$this->ipsclass->Error(array('MSG'=>'trade_already_made_offer'));
		}
		
		$this->ipsclass->input['offer_money'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['offer_money'],'int');
		$this->ipsclass->input['offer_money'] = $this->ipsclass->input['offer_money'] > 0 ? $this->ipsclass->input['offer_money'] : 0;
		
		if($this->ipsclass->numbers->compare($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->input['offer_money']) < 0)
		{
			$this->ipsclass->Error(array('MSG'=>'trade_not_enough_money'));
		}
		
		if($selling['trade_exact'])
		{
			if($this->ipsclass->numbers->compare($this->ipsclass->input['offer_money'],$selling['want_money']) < 0)
			{
				$this->ipsclass->Error(array('MSG'=>'trade_not_enough_money_offered'));
			}
			
			$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$this->ipsclass->input['sell']));
			$this->ipsclass->DB->exec_query();
			while($want = $this->ipsclass->DB->fetch_row())
			{
				$count = 0;
				if(is_array($this->ipsclass->market_class->inventory[$want['item']]))
				{
					foreach($this->ipsclass->market_class->inventory[$want['item']] as $inv_id => $item)
					{
						if(isset($this->ipsclass->input['inv_id'][$inv_id]) && $this->ipsclass->input['inv_id'][$inv_id] == 1)
						{
							$count++;
						}
					}
				}
				if($count < $want['quanity'])
				{
					$this->ipsclass->Error(array('MSG'=>'trade_not_enough_items_offered'));
				}
			}
		}
		
		$this->ipsclass->DB->do_insert('market_trade_offers',array(
																	't_id' => $this->ipsclass->input['sell'],
																	'member_id' => $this->ipsclass->member['id'],
																	'offer_money' => $this->ipsclass->input['offer_money'],
																	'offer_time' => time(),
																	'ip_address' => $this->ipsclass->ip_address,
																	'picked' => 0,
										)						);
		
		foreach($this->ipsclass->market_class->inventory_id as $inv_id => $item)
		{
			if(isset($this->ipsclass->input['inv_id'][$inv_id]) && $this->ipsclass->input['inv_id'][$inv_id] == 1)
			{
				$this->ipsclass->DB->do_insert('market_trade_offer_items',array(
																					't_id' => $this->ipsclass->input['sell'],
																					'member_id' => $item['member_id'],
																					'item' => $item['item'],
																					'price_bought' => $item['price_bought'],
																					'resell_price' => $item['resell_price'],
																					'time_bought' => $item['time_bought'],
																					'settings' => $item['settings'],
																					'expired' => $item['expired'],
												)								);
				$this->ipsclass->DB->do_delete('market_inventory',"i_id=".$inv_id);
			}
		}
		
		if($this->ipsclass->input['offer_money'])
		{
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->input['offer_money']),
					),"id=".$this->ipsclass->member['id']);
		}
		
		$this->ipsclass->market_class->redirect('trade_offer_created',"plugin=".$this->ipsclass->input['plugin']."&do=selling&sell=".$this->ipsclass->input['sell']);
	}
	
	function run_list_offers()
	{
		$LIMIT = 50;
		
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling || $selling['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']));
		$page_links = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																'PER_PAGE'     => $LIMIT,
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=offers&amp;sell=".$this->ipsclass->input['sell'],0,'st'),
																'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=offers&amp;sell=".$this->ipsclass->input['sell'],
																'USE_ST'       => 'st'
														)	);
		
		$offers = array();
		$this->ipsclass->DB->build_query(array(
												'select' => "o.*",
												'from' => array('market_trade_offers' => 'o'),
												'add_join' => array(
																		array(
																				'select' => "m.members_display_name, m.mgroup",
																				'from' => array('members' => 'm'),
																				'where' => "m.id=o.member_id",
																				'type' => 'left',
																			),
																	),
												'where' => "t_id=".$this->ipsclass->input['sell'],
												'order' => "offer_time DESC",
												'limit' => array($this->ipsclass->input['st'],$LIMIT),
										)	);
		$this->ipsclass->DB->exec_query();
		while($offer = $this->ipsclass->DB->fetch_row())
		{
			$offer['member_name'] = $this->ipsclass->market_class->format_name($offer['member_id'],$offer['members_display_name'],$offer['mgroup']);
			$offer['offer_time'] = $this->ipsclass->get_date($offer['offer_time'],'LONG');
			$offer['offer_money'] = $this->ipsclass->numbers->format_currency($offer['offer_money']);
			$offer['items'] = array();
			$offers[$offer['member_id']] = $offer;
		}
		
		if($offers)
		{
			$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_offer_items','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id IN (".implode(", ",array_keys($offers)).")"));
			$this->ipsclass->DB->exec_query();
			while($item = $this->ipsclass->DB->fetch_row())
			{
				$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
				$item['fprice_bought'] = $item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price_bought']);
				$item['fresell_price'] = $item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item['resell_price']);
				$offers[$item['member_id']]['items'][] = $item;
			}
		}
		
		foreach($offers as $mid => $offer)
		{
			$offers[$mid]['count_items'] = $this->ipsclass->do_number_format(count($offer['items']));
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_list_offers($offers, $page_links);
	}
	
	function run_pick_offer()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		$this->ipsclass->input['member_id'] = intval($this->ipsclass->input['member_id']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling || $selling['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$theOffer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->input['member_id']));
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$this->ipsclass->input['member_id']));
		
		if(!$theOffer || !$member)
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$selling['time_sold'] = time();
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_offer_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if($item['member_id'] == $this->ipsclass->input['member_id'])
			{
				$item['member_id'] = $this->ipsclass->member['id'];
			}
			$this->ipsclass->DB->do_insert('market_inventory',array(
																		'member_id' => $item['member_id'],
																		'item' => $item['item'],
																		'price_bought' => $item['price_bought'],
																		'resell_price' => $item['resell_price'],
																		'time_bought' => $item['time_bought'],
																		'settings' => $item['settings'],
																		'expired' => $item['expired'],
											)						);
		}
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_insert('market_inventory',array(
																		'member_id' => $this->ipsclass->input['member_id'],
																		'item' => $item['item'],
																		'price_bought' => $item['price_bought'],
																		'resell_price' => $item['resell_price'],
																		'time_bought' => $item['time_bought'],
																		'settings' => $item['settings'],
																		'expired' => $item['expired'],
											)						);
			$this->ipsclass->DB->do_insert('market_trade_log_items',array(
																			't_id' => $item['t_id'],
																			'member_id' => $item['member_id'],
																			'item' => $item['item'],
											)							);
		}
		$this->ipsclass->DB->do_delete('market_trade_selling_items',"t_id=".$this->ipsclass->input['sell'] );
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'o.*',
												'from' => array('market_trade_offers'=>'o'),
												'add_join' => array(
																		array(
																				'select' => 'm.'.$this->ipsclass->vars['market_members_field'],
																				'from' => array('members'=>'m'),
																				'where' => 'm.id=o.member_id',
																				'type' => 'left',
																			),
																	),
												'where' => "o.t_id=".$this->ipsclass->input['sell'],
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($offer = $this->ipsclass->DB->fetch_row($q))
		{
			if($offer['member_id'] == $this->ipsclass->input['member_id'])
			{
				continue;
			}
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($offer[$this->ipsclass->vars['market_members_field']],$offer['offer_money']),
								),"id=".$offer['member_id']);
		}
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_wants','where'=>"t_id=".$this->ipsclass->input['sell']));
		$q = $this->ipsclass->DB->exec_query();
		while($want = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_insert('market_trade_log_wants',$want);
		}
		$this->ipsclass->DB->do_delete('market_trade_selling_wants',"t_id=".$this->ipsclass->input['sell']);
		
		$this->ipsclass->DB->do_update('members',array(
														$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($member[$this->ipsclass->vars['market_members_field']],$selling['selling_money']),
						),"id=".$theOffer['member_id']);
		$this->ipsclass->DB->do_update('members',array(
														$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$theOffer['offer_money']),
				),"id=".$this->ipsclass->member['id']);
		$this->ipsclass->DB->do_update('market_trade_offers',array(
																	'picked' => -1,
						),"t_id=".$this->ipsclass->input['sell']);
		$this->ipsclass->DB->do_update('market_trade_offers',array(
																	'picked' => 1,
		),"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->input['member_id']);
		$this->ipsclass->DB->do_insert('market_trade_logs',$selling);
		$this->ipsclass->DB->do_delete('market_trade_selling',"t_id=".$this->ipsclass->input['sell']);
		
		$this->ipsclass->market_class->redirect('trade_offer_picked',"plugin=".$this->ipsclass->input['plugin']);
	}
	
	function run_manage_selling()
	{
		$LIMIT = 50;
		
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>"COUNT(*)",'from'=>'market_trade_selling','where'=>"member_id=".$this->ipsclass->member['id']));
		$page_links = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																'PER_PAGE'     => $LIMIT,
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=manage",0,'st'),
																'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=manage",
																'USE_ST'       => 'st'
														)	);
		
		$selling = array();
		$this->ipsclass->DB->build_query(array(
												'select' => "s.*",
												'from' => array('market_trade_selling' => 's'),
												'add_join' => array(
																		array(
																				'select' => "COUNT(i.i_id) as items",
																				'from' => array('market_trade_selling_items' => 'i'),
																				'where' => "i.t_id=s.t_id",
																				'type' => 'left',
																			),
																	),
												'where' => "s.member_id=".$this->ipsclass->member['id'],
												'group' => 'i.t_id',
												'order' => "s.time_selling ASC",
												'limit' => array($this->ipsclass->input['st'],$LIMIT)
										)	);
		$this->ipsclass->DB->exec_query();
		while($sell = $this->ipsclass->DB->fetch_row())
		{
			$sell['items'] = $this->ipsclass->numbers->do_number_format($sell['items']);
			$sell['offers'] = 0;
			$sell['time_selling'] = $this->ipsclass->get_date($sell['time_selling'],'LONG');
			$sell['selling_money'] = $this->ipsclass->numbers->format_currency($sell['selling_money']);
			$sell['want_money'] = $this->ipsclass->numbers->format_currency($sell['want_money']);
			
			$selling[$sell['t_id']] = $sell;
		}
		
		if(count($selling))
		{
			$this->ipsclass->DB->build_query(array('select'=>"COUNT(*) as offers, t_id",'from'=>'market_trade_offers','where'=>"t_id IN (".implode(", ",array_keys($selling)).")",'group'=>'t_id'));
			$this->ipsclass->DB->exec_query();
			while($offers = $this->ipsclass->DB->fetch_row())
			{
				$selling[$offers['t_id']]['offers'] = $this->ipsclass->numbers->do_number_format($offers['offers']);
			}
		}
		
		$this->ipsclass->lang['trade_selling_and_want_money'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_selling_and_want_money']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_manage_selling($selling, $page_links);
	}
	
	function run_list_sold()
	{
		$LIMIT = 50;
		
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>"COUNT(*)",'from'=>'market_trade_logs','where'=>"member_id=".$this->ipsclass->member['id']));
		$page_links = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																'PER_PAGE'     => $LIMIT,
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=sold",0,'st'),
																'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=sold",
																'USE_ST'       => 'st'
														)	);
		
		$sold = array();
		$this->ipsclass->DB->build_query(array(
												'select' => "s.*",
												'from' => array('market_trade_logs' => 's'),
												'add_join' => array(
																		array(
																				'select' => "o.offer_money",
																				'from' => array('market_trade_offers' => 'o'),
																				'where' => "s.t_id=o.t_id AND o.picked=1",
																				'type' => 'inner',
																			),
																		array(
																				'select' => "COUNT(i.member_id) as items",
																				'from' => array('market_trade_log_items' => 'i'),
																				'where' => "i.t_id=o.t_id",
																				'type' => 'left',
																			),
																		array(
																				'select' => "m.members_display_name, m.mgroup",
																				'from' => array('members' => 'm'),
																				'where' => "m.id=o.member_id",
																				'type' => 'left',
																			),
																	),
												'where' => "s.member_id=".$this->ipsclass->member['id'],
												'group' => 'i.t_id',
												'order' => "s.time_sold DESC",
												'limit' => array($this->ipsclass->input['st'],$LIMIT)
										)	);
		$this->ipsclass->DB->exec_query();
		while($sell = $this->ipsclass->DB->fetch_row())
		{
			$sell['items'] = $this->ipsclass->numbers->do_number_format($sell['items']);
			$sell['offers'] = 0;
			$sell['time_selling'] = $this->ipsclass->get_date($sell['time_selling'],'LONG');
			$sell['time_sold'] = $this->ipsclass->get_date($sell['time_sold'],'LONG');
			$sell['selling_money'] = $this->ipsclass->numbers->format_currency($sell['selling_money']);
			$sell['want_money'] = $this->ipsclass->numbers->format_currency($sell['want_money']);
			$sell['offer_money'] = $this->ipsclass->numbers->format_currency($sell['offer_money']);
			$sell['offerer'] = $this->ipsclass->market_class->format_name($sell['member_id'],$sell['members_display_name'],$sell['mgroup']);
			
			$sold[$sell['t_id']] = $sell;
		}
		
		if(count($sold))
		{
			$this->ipsclass->DB->build_query(array('select'=>"COUNT(*) as offers, t_id",'from'=>'market_trade_offers','where'=>"t_id IN (".implode(", ",array_keys($sold)).")",'group'=>'t_id'));
			$this->ipsclass->DB->exec_query();
			while($offers = $this->ipsclass->DB->fetch_row())
			{
				$sold[$offers['t_id']]['offers'] = $this->ipsclass->numbers->do_number_format($offers['offers']);
			}
		}
		
		$this->ipsclass->lang['trade_selling_want_gained_money'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_selling_want_gained_money']);
		$this->ipsclass->lang['trade_offerer_money'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_offerer_money']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_list_sold($sold, $page_links);
	}
	
	function run_view_sold()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		$sold = $this->ipsclass->DB->build_and_exec_query(array(
																'select' => 's.*',
																'from' => array('market_trade_logs'=>'s'),
																'add_join' => array(
																						array(
																								'select' => 'm.members_display_name,m.mgroup',
																								'from' => array('members' => 'm'),
																								'where' => 'm.id=s.member_id',
																								'type' => 'left',
																							),
																					),
																'where' => "s.t_id=".$this->ipsclass->input['sell'],
														)	);
		
		if(!$sold || $sold['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'missing_files'));
		}
		
		$sold['member_name'] = $this->ipsclass->market_class->format_name($sold['member_id'],$sold['members_display_name'],$sold['mgroup']);
		$sold['time_selling'] = $this->ipsclass->get_date($sold['time_selling'],'LONG');
		$sold['time_sold'] = $this->ipsclass->get_date($sold['time_sold'],'LONG');
		$sold['selling_money'] = $this->ipsclass->numbers->format_currency($sold['selling_money']);
		$sold['want_money'] = $this->ipsclass->numbers->format_currency($sold['want_money']);
		$sold['trade_exact'] = $sold['trade_exact'] ? $this->ipsclass->lang['trade_offer_match'] : $this->ipsclass->lang['trade_offer_anything'];
		$sold['description'] = $this->ipsclass->parser->pre_display_parse($sold['description']);
		
		$items = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_log_items','where'=>"t_id=".$sold['t_id']));
		$this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row())
		{
			$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_store_description()));
			$items[] = $item;
		}
		
		$sold['no_items'] = count($items);
		
		$wanted = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_log_wants','where'=>"t_id=".$sold['t_id']));
		$this->ipsclass->DB->exec_query();
		while($want = $this->ipsclass->DB->fetch_row())
		{
			$want['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$want['item']]->parse_store_description()));
			$want['quanity'] = $this->ipsclass->do_number_format($want['quanity']);
			$wanted[] = $want;
		}
		
		$this->ipsclass->lang['trade_money_trading'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_money_trading']);
		$this->ipsclass->lang['trade_money_want'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_money_want']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_sold_detail_page($sold, $items, $wanted);
	}
	
	function run_sold_offers()
	{
		$LIMIT = 50;
		
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_logs','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling || $selling['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']));
		$page_links = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																'PER_PAGE'     => $LIMIT,
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=offers&amp;sell=".$this->ipsclass->input['sell'],0,'st'),
																'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=offers&amp;sell=".$this->ipsclass->input['sell'],
																'USE_ST'       => 'st'
														)	);
		
		$offers = array();
		$this->ipsclass->DB->build_query(array(
												'select' => "o.*",
												'from' => array('market_trade_offers' => 'o'),
												'add_join' => array(
																		array(
																				'select' => "m.members_display_name, m.mgroup",
																				'from' => array('members' => 'm'),
																				'where' => "m.id=o.member_id",
																				'type' => 'left',
																			),
																	),
												'where' => "t_id=".$this->ipsclass->input['sell'],
												'order' => "o.picked DESC, o.offer_time DESC",
												'limit' => array($this->ipsclass->input['st'],$LIMIT),
										)	);
		$this->ipsclass->DB->exec_query();
		while($offer = $this->ipsclass->DB->fetch_row())
		{
			$offer['member_name'] = $this->ipsclass->market_class->format_name($offer['member_id'],$offer['members_display_name'],$offer['mgroup']);
			$offer['offer_time'] = $this->ipsclass->get_date($offer['offer_time'],'LONG');
			$offer['offer_money'] = $this->ipsclass->numbers->format_currency($offer['offer_money']);
			$offer['items'] = array();
			$offers[$offer['member_id']] = $offer;
		}
		
		if($offers)
		{
			$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_offer_items','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id IN (".implode(", ",array_keys($offers)).")"));
			$this->ipsclass->DB->exec_query();
			while($item = $this->ipsclass->DB->fetch_row())
			{
				$item['fprice_bought'] = $item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price_bought']);
				$item['fresell_price'] = $item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item['resell_price']);
				$offers[$item['member_id']]['items'][] = $item;
			}
		}
		
		foreach($offers as $mid => $offer)
		{
			$offers[$mid]['count_items'] = $this->ipsclass->do_number_format(count($offer['items']));
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_sold_offers($offers, $page_links);
	}
	
	function run_manage_offers()
	{
		$LIMIT = 50;
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_trade_offers','where'=>"member_id=".$this->ipsclass->member['id']." AND picked=0"));
		$page_links = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																'PER_PAGE'     => $LIMIT,
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=offer_manage",0,'st'),
																'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=offer_manage",
																'USE_ST'       => 'st'
														)	);
		
		$this->ipsclass->DB->build_query(array(
												'select' => "o.*",
												'from' => array('market_trade_offers'=>'o'),
												'where' => "o.member_id=".$this->ipsclass->member['id']." AND o.picked=0",
												'add_join' => array(
																		array(
																				'select' => "s.*",
																				'from' => array('market_trade_selling'=>'s'),
																				'where' => "s.t_id=o.t_id",
																				'type' => 'left',
																			),
																		array(
																				'select' => "m.members_display_name, m.mgroup",
																				'from' => array('members'=>'m'),
																				'where' => "m.id=s.member_id",
																				'type' => 'left',
																			),
																		array(
																				'select' => "COUNT(i.t_id) as items_offered",
																				'from' => array('market_trade_offer_items'=>'i'),
																				'where' => "i.t_id=o.t_id AND i.member_id=o.member_id",
																				'type' => 'left',
																			),
																	),
												'group' => "i.t_id",
												'order' => "offer_time ASC",
												'limit' => array($this->ipsclass->input['st'],$LIMIT),
										)	);
		$this->ipsclass->DB->exec_query();
		$offers = array();
		while($offer = $this->ipsclass->DB->fetch_row())
		{
			$offer['items_offered'] = $this->ipsclass->numbers->do_number_format($offer['items_offered']);
			$offer['seller'] = $this->ipsclass->market_class->format_name($offer['member_id'],$offer['members_display_name'],$offer['mgroup']);
			$offer['offer_time'] = $this->ipsclass->get_date($offer['offer_time'],'LONG');
			$offer['time_selling'] = $this->ipsclass->get_date($offer['time_selling'],'LONG');
			$offer['selling_money'] = $this->ipsclass->numbers->format_currency($offer['selling_money']);
			$offer['offer_money'] = $this->ipsclass->numbers->format_currency($offer['offer_money']);
			$offer['items'] = array();
			$offers[$offer['t_id']] = $offer;
		}
		
		if(count($offers))
		{
			$this->ipsclass->DB->build_query(array('select'=>"COUNT(*) as items_selling, t_id",'from'=>'market_trade_selling_items','where'=>"t_id IN (".implode(", ",array_keys($offers)).")",'group'=>'t_id'));
			$this->ipsclass->DB->exec_query();
			while($items = $this->ipsclass->DB->fetch_row())
			{
				$offers[$items['t_id']]['items_selling'] = $this->ipsclass->numbers->do_number_format($items['items_selling']);
			}
			$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_offer_items','where'=>"member_id=".$this->ipsclass->member['id']." AND t_id IN (".implode(", ",array_keys($offers)).")"));
			$this->ipsclass->DB->exec_query();
			while($item = $this->ipsclass->DB->fetch_row())
			{
				$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
				$item['fprice_bought'] = $item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price_bought']);
				$item['fresell_price'] = $item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item['resell_price']);
				$offers[$item['t_id']]['items'][] = $item;
			}
		}
		
		$this->ipsclass->lang['trade_selling_offered_money'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_selling_offered_money']);
		$this->ipsclass->lang['trade_money_offered'] = str_replace("<#CURRENCY#>",$this->ipsclass->vars['market_currency'],$this->ipsclass->lang['trade_money_offered']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->trade_manage_offers($offers, $page_links);
	}
	
	function run_remove_offer()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling || $selling['member_id'] == $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$theOffer = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_offers','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->member['id']));
		
		if(!$theOffer)
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_offer_items','where'=>"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->member['id']));
		$i = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($i))
		{
			$inv_item = array(
								'member_id'		=> $item['member_id'],
								'item'			=> $item['item'],
								'price_bought'	=> $item['price_bought'],
								'resell_price'	=> $item['resell_price'],
								'time_bought'	=> $item['time_bought'],
								'settings'		=> $item['settings'],
								'expired'		=> $item['expired'],
							);
			
			$this->ipsclass->DB->do_insert('market_inventory',$inv_item);
		}
		$this->ipsclass->DB->do_delete('market_trade_offer_items',"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->member['id']);
		
		$this->ipsclass->DB->do_update('members',array(
														$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$theOffer['offer_money']),
				),"id=".$this->ipsclass->member['id']);
		
		$this->ipsclass->DB->do_delete('market_trade_offers',"t_id=".$this->ipsclass->input['sell']." AND member_id=".$this->ipsclass->member['id']);
		
		$this->ipsclass->market_class->redirect('trade_offer_removed',"plugin=".$this->ipsclass->input['plugin']);
	}
	
	function run_delete_trade()
	{
		$this->ipsclass->input['sell'] = intval($this->ipsclass->input['sell']);
		$this->ipsclass->input['member_id'] = intval($this->ipsclass->input['member_id']);
		
		$selling = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_trade_selling','where'=>"t_id=".$this->ipsclass->input['sell']));
		
		if(!$selling || $selling['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_offer_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_insert('market_inventory',array(
																		'member_id' => $item['member_id'],
																		'item' => $item['item'],
																		'price_bought' => $item['price_bought'],
																		'resell_price' => $item['resell_price'],
																		'time_bought' => $item['time_bought'],
																		'settings' => $item['settings'],
																		'expired' => $item['expired'],
											)						);
		}
		$this->ipsclass->DB->do_delete('market_trade_offer_items',"t_id=".$this->ipsclass->input['sell'] );
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_trade_selling_items','where'=>"t_id=".$this->ipsclass->input['sell']));
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_insert('market_inventory',array(
																		'member_id' => $this->ipsclass->member['id'],
																		'item' => $item['item'],
																		'price_bought' => $item['price_bought'],
																		'resell_price' => $item['resell_price'],
																		'time_bought' => $item['time_bought'],
																		'settings' => $item['settings'],
																		'expired' => $item['expired'],
											)						);
		}
		$this->ipsclass->DB->do_delete('market_trade_selling_items',"t_id=".$this->ipsclass->input['sell'] );
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'o.*',
												'from' => array('market_trade_offers'=>'o'),
												'add_join' => array(
																		array(
																				'select' => 'm.'.$this->ipsclass->vars['market_members_field'],
																				'from' => array('members'=>'m'),
																				'where' => 'm.id=o.member_id',
																				'type' => 'left',
																			),
																	),
												'where' => "o.t_id=".$this->ipsclass->input['sell'],
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($offer = $this->ipsclass->DB->fetch_row($q))
		{
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($offer[$this->ipsclass->vars['market_members_field']],$offer['offer_money']),
								),"id=".$offer['member_id']);
		}
		$this->ipsclass->DB->do_delete('market_trade_offers',"t_id=".$this->ipsclass->input['sell'] );
		
		$this->ipsclass->DB->do_delete('market_trade_selling_wants',"t_id=".$this->ipsclass->input['sell']);
		
		$this->ipsclass->DB->do_update('members',array(
														$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$selling['selling_money']),
				),"id=".$this->ipsclass->member['id']);
		$this->ipsclass->DB->do_delete('market_trade_selling',"t_id=".$this->ipsclass->input['sell']);
		
		$this->ipsclass->market_class->redirect('trade_trade_deleted',"plugin=".$this->ipsclass->input['plugin']);
	}
}
?>